package com.lm.scau.framedemos.Activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.lm.scau.framedemos.BaseActivity;
import com.lm.scau.framedemos.Cache.BitmapCache;
import com.lm.scau.framedemos.R;
import com.lm.scau.framedemos.Views.LMTitleBar;
import com.lm.scau.framedemos.Views.SunmaryView;

import org.json.JSONObject;

import java.util.Map;

/**
 * Function:
 * auth：liming on 2016/7/14 09:33
 * mail：liming@wxchina.com
 */
public class VolleyActivity extends BaseActivity {
    private SunmaryView sunmaryView;
    private TextView requestTextView;
    private RequestQueue mQueue;
    private LinearLayout imageLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_volley);
        mQueue = Volley.newRequestQueue(this);
        initView();
        initAction();
    }

    private void initAction() {
        sunmaryView.getBtnOk().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageRequest ir = new ImageRequest("http://www.dpfile.com/s/res/app-touch-icon-152x152.ee6d0c24fc2de0f9a62b6cc9e6720393.png", new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap response) {
                        // imageView.setImageBitmap(response);
                        ImageView iv = new ImageView(VolleyActivity.this);
                        //   iv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                        iv.setImageBitmap(response);
                        imageLayout.addView(iv);
                    }
                }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
                mQueue.add(ir);
            }
        });
        sunmaryView.getBtnCancel().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringRequest request = new StringRequest("http://www.baidu.com", new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        requestTextView.setText(response);

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
                mQueue.add(request);
            }
        });
    }

    private void initView() {
        mTitleBar = (LMTitleBar) findViewById(R.id.titlebar);
        sunmaryView = (SunmaryView) findViewById(R.id.sunmary_view);
        requestTextView = (TextView) findViewById(R.id.text_view_request_info);
        imageLayout = (LinearLayout) findViewById(R.id.image_layout_2);
        mTitleBar.setTitle("Volley");
        sunmaryView.setTitle("Volley");
        sunmaryView.setSunmary("使得Android应用网络操作更方便更快捷；抽象了底层Http Client等实现的细节，让开发者更专注与产生RESTful Request");
        sunmaryView.getBtnOk().setText("ImageRequest");
        sunmaryView.getBtnCancel().setText("StringRequest");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mQueue.stop();
    }

    public void displayImg(View view) {
        // ImageView imageView = (ImageView)this.findViewById(R.id.image_view);
        RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());

        ImageLoader imageLoader = new ImageLoader(mQueue, new BitmapCache());

        //  ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView,R.drawable.default_image, R.drawable.default_image);
        //  imageLoader.get("http://developer.android.com/images/home/aw_dac.png", listener);
        //指定图片允许的最大宽度和高度
        //imageLoader.get("http://developer.android.com/images/home/aw_dac.png",listener, 200, 200);
        StringRequest request = new StringRequest(Request.Method.POST, "", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

            }
        }, new Response.ErrorListener() {


            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return super.getParams();
            }
        };
        mQueue.add(request);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("", null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        mQueue.add(jsonObjectRequest);
    }
}
